#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char const *argv[])
{

    // 1 打开一个文件
    int fd = open("/home/wupengwei/xiyouji.txt", O_RDONLY);
    if (fd == -1)
    {
        perror("open error");
        return -1;
    }

    int fd_dest = open("/home/wupengwei/xiyouji_copy.txt", O_WRONLY | O_CREAT | O_APPEND, 0644);
    if (fd_dest == -1)
    {
        perror("open error");
        return -1;
    }

    // 2 循环读取该文件的内容
    char buff[1024] = {0};
    int red_size = 0;
    while ((red_size = read(fd, buff, sizeof(buff))) > 0)
    {
        // 3 读取过程中同时向一个文件写入
        if (write(fd_dest, buff, red_size) == -1)
        {
            perror("write error");
            return -1;
        }
    }

    // 4 关闭文件
    close(fd);
    close(fd_dest);
    return 0;
}
